﻿using System;
using System.Xml;
using System.Collections.Generic;

//自定义命名空间
using DreamCube.Foundation.Basic.Utility;
using DreamCube.Foundation.Basic.Objects.EqualityComparers;

namespace DreamCube.Foundation.Validate
{
    public static class ValidateRuleHelper
    {
        #region "字段"

        /// <summary>
        /// 验证规则的缓冲区
        /// </summary>
        private static Dictionary<String, ValidateRule> validateRulesCache =
                                    new Dictionary<String, ValidateRule>(new StringEqualityIgnoreCaseComparerGeneric());

        #endregion

        #region "静态方法"

        /// <summary>
        /// 静态构造方法
        /// </summary>
        static ValidateRuleHelper()
        {
            try
            {
                String xmlFilePath = "{0}\\Objects\\Validate\\DreamCube.Foundation.Basic.ValidateRules.xml";
                XmlDocument doc = new XmlDocument();
                doc.Load(String.Format(xmlFilePath, MyDll.GetCurrentDllPath()));
                XmlNode node = doc.SelectSingleNode("/Rules");
                if (node != null)
                {
                    XmlNodeList nodes = node.SelectNodes("./Rule");
                    if (nodes != null && nodes.Count > 0)
                    {
                        for (Int32 i = 0, j = nodes.Count; i < j; i++)
                        {
                            ValidateRule rule = new ValidateRule();
                            MyObject.LoadPropertyValueFromXmlNode(nodes[i], rule);
                            validateRulesCache.Add(nodes[i].Attributes["Key"].Value.ToString(), rule);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MyLog.MakeLog(ex);
            }
        }

        /// <summary>
        /// 根据Key值获取验证规则
        /// </summary>
        /// <param name="key"></param>
        /// <returns>验证规则的正则表达式</returns>
        public static String GetRegexPatternByKey(String key)
        {
            if (validateRulesCache.ContainsKey(key))
                return validateRulesCache[key].RegexPattern;
            return String.Empty;
        }

        /// <summary>
        /// 根据Key值获取验证规则
        /// </summary>
        /// <param name="key"></param>
        /// <returns>ValidateRule验证规则对象</returns>
        public static ValidateRule GetValidateRuleByKey(String key)
        {
            if (validateRulesCache.ContainsKey(key))
                return validateRulesCache[key];
            return null;
        }

        /// <summary>
        /// 根据key值更新正则表达式；
        /// 注意：此更新只更新当前程序域中的正则表达式，对于其他程序域中的此方法不做任何影响的，
        /// 并且更新只保存在内存中，下次重启程序还是恢复到默认值
        /// </summary>
        /// <param name="key"></param>
        public static void UpdateRegexPatternByKey(String key, String regexPattern)
        {
            if (validateRulesCache.ContainsKey(key))
                validateRulesCache[key].RegexPattern = regexPattern;
        }

        /// <summary>
        /// 添加验证对象到缓存中
        /// </summary>
        /// <param name="key"></param>
        /// <param name="regexPattern"></param>
        /// <param name="descriptions"></param>
        /// <returns>是否添加成功</returns>
        public static Boolean AddRegexPattern(String key, String regexPattern, String descriptions = "")
        {
            ValidateRule rule = new ValidateRule();
            rule.Key = key;
            rule.RegexPattern = regexPattern;
            rule.Description = descriptions;
            return AddRegexPattern(rule);
        }

        /// <summary>
        /// 添加验证对象到缓存中
        /// </summary>
        /// <param name="rule"></param>
        /// <returns>是否添加成功</returns>
        public static Boolean AddRegexPattern(ValidateRule rule)
        {
            if (rule == null || String.IsNullOrEmpty(rule.Key) || String.IsNullOrEmpty(rule.RegexPattern)) 
                return false;
            if (validateRulesCache.ContainsKey(rule.Key))
                return false;
            else
                validateRulesCache.Add(rule.Key, rule);
            return true;
        }

        #endregion
    }
}
